package mz.behaveas.model.ducktype {

	/**
	 * Function checking if two Arrays are equal.
	 * 
	 * @author maxim.zaks@gmail.com
	 */
	public function arraysAreEqual(a1 : Array, a2 : Array) : Boolean {
		
		if(a1.length != a2.length) {
			return false;
		}
		
		for (var iter1 : String in a1) {
			
			if(!equal(a1[iter1], a2[iter1])){
				return false;
			}
		}
		
		// Because arrays could have associative keys
		for (var iter2 : String in a2) {
			if(!equal(a1[iter2], a2[iter2])){
				return false;
			}
		}
		
		return true;
	}
}

import mz.behaveas.model.ducktype.Duck;
import mz.behaveas.model.ducktype.arraysAreEqual;

function equal(child1 : *, child2 : *) : Boolean {
	if(child1 is Array && child2 is Array) {
		return arraysAreEqual(child1, child2);
	} 
	
	if(child1 is Duck){
		return Duck(child1).equals(child2);
	}
	
	if(child2 is Duck){
		return Duck(child2).equals(child1);
	}
	
	return child1 == child2;
}